$(function(){
	initBlockUI();
   showUsergroupList();
});

// 显示公司信息
function showUsergroupList()
{
   var url = "company_power.php";
   var pars = { "page_status": 1 };
   $.post(url, pars, function(xmlobj){
	  $("#tbody2").html("");
	  $(xmlobj).find("item").each(function(i){
		 var sec_id = $(this).find("id").text();
		 var zhname = $(this).find("zhname").text();
		 var power = $(this).find("power").text();
		 $("<tr></tr>")
		   .append($("<th></th>").html(i + 1))
		   .append($("<td></td>").html(zhname))
		   .append($("<td></td>")
				   .html($("<a href=\"#\"></a>")
				   		.attr("id", "view_" + sec_id)
				   		.attr("uid", sec_id)
				   		.attr("power", power)
				   		.html("查看")
				   		.click(function(){
				   			showPower($(this).attr("uid"), $(this).attr("power"));
				   			return false;
				   		})
				   ))
		   .append($("<td></td>")
				   .html($("<a href=\"#\"></a>")
				   		.attr("id", "modify_" + sec_id)
				   		.attr("uid", sec_id)
				   		.attr("power", power)
				   		.html("修改")
				   		.click(function(){
				   			modifyPower($(this).attr("uid"), $(this).attr("power"));
				   			return false;
				   		})
				   ))
		   .appendTo("#tbody2");
	  });
	  tablePretty("#tbody2");
   });
}

// 显示部门列表
function showSectionList(comid)
{
   var url = "company_power.php";
   var pars = { "page_status": 2,
		"company_id": comid
   };
   $("#tbody2").html("");
   $.post(url, pars, function(xmlobj){
	  $(xmlobj).find("item").each(function(){
		 var sec_id = $(this).find("id").text();
		 var zhname = $(this).find("zhname").text();
		 var power = $(this).find("power").text();
		 $("<tr></tr>").append($("<td></td>").html(zhname))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\" id=\"view_" + sec_id + "\""
						 + " onclick=\"showPower(" + sec_id + ");\" power=\""
						 + power + "\">查看</a>"))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\""
						 + " onclick=\"modifyPower(" + sec_id + ");\">修改</a>"))
		   .appendTo("#tbody2");
	  });
   });
}

// 显示权限
function showPower(uid, power)
{
   // var power_array = $("#view_" + sec_id).attr("power").split(",").sort();
   var power_array = arrayUnique(power.split(",")).sort();
   var power_list = $("<div></div>")
	 .addClass("power_list")
	 .append($("<div class=\"modal_header\"></div>").html("现有权限："))
	 .append($("<div class=\"modal_content\"></div>").html(getPowerListViaJs(power_array)));
   $.modal(power_list);
}

// 修改权限
function modifyPower(cid, power)
{
	var t = [];
	$.each(__powerArray__, function(i, n) {
		t[t.length] = n[3];
	});
	var power_selected = arrayUnique($("#modify_" + cid).attr("power").split(","));
	var power_block = $("<div></div>").addClass("modify_modal")
		.append($("<div></div>").addClass("modal_header").html("修改权限"))
		.append($("<div></div>").addClass("modal_content").html(makePowerCheckBox(t, power_selected, "power_check", "pc[]")))
		.append($("<div></div>").addClass("modal_footer").html(
			$("<input type=\"button\" />").val("提交").click(function() {
				var power = [];
				$("#power_check input:checkbox[@name='pc\[\]']:checked").each(function(i) {
					power[power.length] = $(this).val();
				});
				var url = "company_power.php";
				var pars = { page_status: 2, cid: cid, power: power.join(",") };
				$.post(url, pars, function(text) {
					if (/^Success/.test(text)) { $("#view_" + cid + ", #modify_" + cid).attr("power", pars["power"]); $.modal.close(); }
				});
			})
		));
	$.modal(power_block);
}
